package per.hnvcam.searchext.controller.administration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import per.hnvcam.hedge.algebra.Hedge2AlgebraDefinition;
import per.hnvcam.hedge.algebra.HedgeConstants;
import per.hnvcam.searchext.data.dao.ConfigDAO;
import per.hnvcam.searchext.data.model.Config;
import per.hnvcam.searchext.data.model.HedgeConfiguration;

import javax.servlet.http.HttpServletRequest;

@Controller("HedgeController")
public class HedgeController {

    private static final String HEDGE_CONFIGURATION_PAGE = "administration/hedge";

    @Autowired
    @Qualifier("ConfigDAO")
    private ConfigDAO configDAO;

    @Autowired
    @Qualifier("Hedge2AlgebraDefinition")
    private Hedge2AlgebraDefinition hedgeAlgebraDefinition;

    @RequestMapping(value = "/administration/hedge.jhtml", method = RequestMethod.GET)
    public String showHedgeConstants(@RequestParam(value = "operation", required = false) String operation, Model model) {
        if (operation != null && operation.equals("check")) {
            String status = "Successfully";
            try {
                hedgeAlgebraDefinition.checkForConsistency();
            } catch (Exception e) {
                status = e.getMessage();
            }
            model.addAttribute("status", status);
        }

        model.addAttribute("hedgeDefinition", hedgeAlgebraDefinition);
        model.addAttribute("config", getHedgeConfiguration());
        model.addAttribute("domain", hedgeAlgebraDefinition.getDomainOfLength(2));

        return HEDGE_CONFIGURATION_PAGE;
    }

    @RequestMapping(value = "/administration/hedge.jhtml", method = RequestMethod.POST)
    public String saveHedgeConstants(HttpServletRequest request,
                                     @ModelAttribute("config") HedgeConfiguration config, Model model) {
        updateHedgeParamValue(HedgeConstants.MANY, String.valueOf(config.getFMValue(HedgeConstants.MANY)));
        updateHedgeParamValue(HedgeConstants.FEW, String.valueOf(config.getFMValue(HedgeConstants.FEW)));
        updateHedgeParamValue(HedgeConstants.VERY, String.valueOf(config.getFMValue(HedgeConstants.VERY)));
        updateHedgeParamValue(HedgeConstants.LITTLE, String.valueOf(config.getFMValue(HedgeConstants.LITTLE)));

        hedgeAlgebraDefinition.clearCache();

        hedgeAlgebraDefinition.setFunctionMeterValue(HedgeConstants.MANY, config.getFMValue(HedgeConstants.MANY));
        hedgeAlgebraDefinition.setFunctionMeterValue(HedgeConstants.FEW, config.getFMValue(HedgeConstants.FEW));
        hedgeAlgebraDefinition.setFunctionMeterValue(HedgeConstants.VERY, config.getFMValue(HedgeConstants.VERY));
        hedgeAlgebraDefinition.setFunctionMeterValue(HedgeConstants.LITTLE, config.getFMValue(HedgeConstants.LITTLE));

        model.addAttribute("config", getHedgeConfiguration());
        model.addAttribute("hedgeDefinition", hedgeAlgebraDefinition);
        model.addAttribute("domain", hedgeAlgebraDefinition.getDomainOfLength(2));
        return HEDGE_CONFIGURATION_PAGE;
    }

    private void updateHedgeParamValue(String name, String value) {
        Config param = configDAO.findByProperty(name);
        if (param == null) {
            param = new Config();
            param.setProperty(name);
        }
        param.setValue(value);
        configDAO.saveOrUpdate(param);
    }

    private HedgeConfiguration getHedgeConfiguration() {
        HedgeConfiguration configuration = new HedgeConfiguration();
        configuration.setAlpha(hedgeAlgebraDefinition.getAlpha());
        configuration.setBeta(hedgeAlgebraDefinition.getBeta());
        getFMValue(configuration, HedgeConstants.MANY);
        getFMValue(configuration, HedgeConstants.FEW);
        getFMValue(configuration, HedgeConstants.VERY);
        getFMValue(configuration, HedgeConstants.LITTLE);
        return configuration;
    }

    private void getFMValue(HedgeConfiguration configuration, String item) {
        configuration.setFMValue(item, hedgeAlgebraDefinition.getFunctionMeterValue(item));
    }

}
